package com.suntop.carousel.qlmediaplay.fragement;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import io.reactivex.functions.Consumer;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.suntop.carousel.R;
import com.suntop.carousel.qlmediaplay.bean.Option;
import com.suntop.carousel.qlmediaplay.bean.PlayerState;
import com.suntop.carousel.qlmediaplay.media.IMediaPlayer;
import com.suntop.carousel.qlmediaplay.picture.QLPictureView;
import com.suntop.carousel.qlmediaplay.tools.MediaHelper;
import com.suntop.carousel.qlmediaplay.video.control.LiteControl;
import com.suntop.carousel.qlmediaplay.video.cover.ImageCover;
import com.suntop.carousel.qlmediaplay.video.player.QLVideoView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MediaFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MediaFragment extends BasicFragment<Option> implements Consumer<PlayerState> {

    private IMediaPlayer iMediaPlayer;

    public MediaFragment() {
        // Required empty public constructor
    }

    public static MediaFragment newInstance(int position,Option t) {
        Bundle args = newBundleInstance(position,t);
        MediaFragment fragment = new MediaFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView(FrameLayout frameLayout, Option option) {
        setProgram_list_id(option.getProgram_list_id());
        setProgram_id(option.getProgram_id());
        registerRxbus();
        if (iMediaPlayer!=null) {
            iMediaPlayer.releasePlay();
            iMediaPlayer = null;
        }
        if (getContext()==null){
            return;
        }
        boolean videoFileType = MediaHelper.isVideoFileType(option.getPath());
        int playType = option.getPlayType();
        if (videoFileType){
            iMediaPlayer=new QLVideoView(getContext())
                    .addIConView(new ImageCover(getContext()),true)
                    .addController(option.isNeedControl()?(new LiteControl(getContext())):null)
                    .setDate(option.getPath())
                    .setScaleType(option.getScaleType())
                    .autoCache(true)
                    .setReBack(true)
                    .setMediaType(playType)
                    .setDelayTime(Math.max(1100,option.getDelayTime()))
                    .setLoop(option.isLoop())
                    .addEventListener(this);

        }else {
            iMediaPlayer=new QLPictureView(getContext()).setDate(option.getPath())
                    .setScaleType(option.getScaleType())
                    .initFarm(option.getDecodeFormat())
                    .setDelayTime(Math.max(1100,option.getDelayTime()))
                    .addEventListener(this);
        }
        frameLayout.addView(iMediaPlayer.getQLView(), ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.MATCH_PARENT);
    }


    @Override
    public void startPlay() {
        if (iMediaPlayer!=null&&isAutoPlay()){
            iMediaPlayer.startPlay();
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        Log.e(" 初始化 "," MediaFragment onStart()="+position+" id=");
        if (iMediaPlayer!=null){
            iMediaPlayer.build(false);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (iMediaPlayer!=null){
            iMediaPlayer.pausePlay();
        }
    }

    @Override
    public void onStop() {
        Log.e(" 初始化 "," MediaFragment onStop()="+position+" id =");

        if (iMediaPlayer!=null){
            iMediaPlayer.pauseTo();
        }
        super.onStop();

    }

    @Override
    public void onDestroy() {
        Log.e(" 初始化 "," NewMediaFragment MediaFragment()="+position+" id =");
        if (iMediaPlayer!=null){
            iMediaPlayer.releasePlay();
        }

        super.onDestroy();
    }
}